// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://cloud.tencent.com/developer/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=hao123.com
// @grant        GM_getResourceText
// @resource myTxt file:///Users/guo/Desktop/test/data.txt
// @require https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.core.min.js
// ==/UserScript==

(function () {
  "use strict";
  var elements = document.getElementsByClassName("cdc-sticky-header__extra");
  if (elements.length == 0) {
    return;
  }
  elements[0].innerHTML =
    elements[0].innerHTML + '<input type="file" id="fileInput"/>';
  var fileInput = document.getElementById("fileInput");
  fileInput.addEventListener("change", (event) => {
    if (event.target.files.length == 0) {
      return;
    }
    var file = event.target.files[0];
    console.log("file: ", file);
    const fileName = file.name;
    const fileArr = fileName.split(".");
    const fileSuffix = fileArr[fileArr.length - 1];
    if (fileSuffix !== "xlsx" && fileSuffix !== "xls") {
      console.log("不支持该格式的解析");
      return;
    }
    var reader = new FileReader();
    reader.onload = (e) => {
      var data = e.target.result;
      var result = XLSX.read(data, {
        type: "binary",
        cellDates: true, // 为了获取excel表格中的时间，返回格式为世界时间
      });
      console.log("read result: ", result);
      let sheetName = result.SheetNames[0];
      var XL_row_object = XLSX.utils.sheet_to_row_object_array(
        result.Sheets[sheetName]
      );
      //生成文件
      createFile("测试.xlsx", XL_row_object);
    };
    reader.onerror = (err) => {
      console.error("读取文件发生错误： ", err);
    };
    reader.readAsBinaryString(file);
  });

  /**
   * 创建文件
   * @param {*} fileName
   * @param {*} data
   * @returns
   */
  const createFile = (fileName, data) => {
    if (!data || data.length == 0) {
      return;
    }
    var ws = XLSX.utils.json_to_sheet(data);
    var workbook = {
      SheetNames: ["sheet"],
      Sheets: {
        sheet: ws,
      },
    };
    XLSX.write(workbook, { bookType: "xlsx", bookSST: true, type: "base64" });
    XLSX.writeFile(workbook, fileName);
  };

  /**
   * 发送post请求
   * @param {*} url
   * @param {*} data
   * @returns
   */
  const postData = (url, data) => {
    return fetch(url, {
      body: JSON.stringify(data), // must match 'Content-Type' header
      cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
      credentials: "same-origin", // include, same-origin, *omit
      headers: {
        "user-agent": "Mozilla/4.0 MDN Example",
        "content-type": "application/json",
      },
      method: "POST", // *GET, POST, PUT, DELETE, etc.
      mode: "cors", // no-cors, cors, *same-origin
      redirect: "follow", // manual, *follow, error
      referrer: "no-referrer", // *client, no-referrer
    }).then((response) => response.json()); // parses response to JSON
  };

  /**
   * 发送get请求
   * @param {*} url
   * @returns
   */
  const getData = (url) => {
    return fetch(url, {
      cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
      credentials: "same-origin", // include, same-origin, *omit

      method: "get", // *GET, POST, PUT, DELETE, etc.
      mode: "cors", // no-cors, cors, *same-origin
      redirect: "follow", // manual, *follow, error
      referrer: "no-referrer", // *client, no-referrer
    }).then((res) => res.json()); // parses response to JSON
  };

//   const decode_contact = async (caseId) => {
//     const res = await postData(decode_url, {
//       type: "7",
//       caseId: caseId,
//       remark: "催收作业需要",
//     });
//     if (res.code != "200") {
//       return 0;
//     }
//     return 1;
//   };
})();
